#pragma strict
static var pontos : int = 0;
static var badThings : int = 0;
static var balance : int = 0;

var speed : float;
var badPuppet : BadPuppet;
var child : Child;
var mainFrame : Texture;
var betweenFrame : Texture;
var endFrame : Texture;
var guiSkin : GUISkin;
var canMove : boolean = true;
var xGUI = 0;
var yGUI = 0;

function Update ()
{
	speed = Input.GetAxis("Horizontal");
	if (Input.GetButtonDown("Fire1"))
		Attack ();
	
	if (speed > 0)
		transform.localEulerAngles.y = 0;
	if (speed < 0)
		transform.localEulerAngles.y = 180;
	
	if (canMove)
		transform.Translate(speed * Time.deltaTime * 50,0,0, Space.World);
	
	if (balance > 4)
	{
		Victory ();
	}
	
	if (balance < -4)
	{
		Defeat ();
	}
		
}

function Attack ()
{
	renderer.material.mainTexture = betweenFrame;
	yield WaitForSeconds (0.1);
	renderer.material.mainTexture = endFrame;
	yield WaitForSeconds (0.1);
	renderer.material.mainTexture = betweenFrame;
	yield WaitForSeconds (0.1);
	renderer.material.mainTexture = mainFrame;

	badPuppet.Attacked();
}

function OnGUI ()
{

	balance = pontos - badThings;
	GUI.skin = guiSkin;
	
	if (!canMove)
	{
		GUI.skin.label.alignment = TextAnchor.MiddleCenter;
		GUI.Box(Rect (xGUI- (Screen.width/4)/2, yGUI-(Screen.height/4)/2, Screen.width/4, Screen.height/4),"");
		GUILayout.BeginArea(Rect (xGUI - (Screen.width/4)/2 , yGUI-(Screen.height/4)/2, Screen.width/4, Screen.height/4 + yGUI/4));
	}
	else
	{
		GUI.skin.label.alignment = TextAnchor.MiddleLeft;
		GUI.Box(Rect (xGUI, yGUI, Screen.width/5, Screen.height/4 + yGUI/2),"");
		GUILayout.BeginArea(Rect (xGUI, yGUI, Screen.width/5, Screen.height/4));
	}
		GUILayout.Box("GameStatus");
		GUILayout.Label ("Bad Actions: " + badThings);
		GUILayout.Label ("Saved Childs: " + pontos);
		GUILayout.Label ("Balance: " + balance);
		if (balance >= 0)
			GUILayout.Label ("Going to HEAVEN\n");
		else
		{
			GUILayout.Label ("Going to HELL\n");
		}
			
		if (!canMove)
		{
			if (GUILayout.Button("PLAY AGAIN"))
				Continue();
		}
	GUILayout.EndArea ();
}

function Victory ()
{
	Pause();
}

function Defeat ()
{
	Pause();
}

function Pause ()
{
	canMove = false;
	badPuppet.isMoving = false;
	child.isMoving = false;
	xGUI = Screen.width/2;
	yGUI = Screen.height/2;
}

function Continue ()
{
	Reset();
	xGUI = 0;
	yGUI = 0;
	canMove = true;
	badPuppet.isMoving = true;
	child.isMoving = true;
}

function Reset ()
{
	pontos = 0;
	badThings = 0;
	balance = 0;
}